package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;


import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FinancialBoutiqueCommonAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.order.base.consts.IKeyValues;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 金融精品网属性类所有的联动处理
 */
@Service
@AttrHandler(FinancialBoutiqueCommonAttrHandler.class)
public class FinancialBoutiqueCommonAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    public static Map<String, IAttrHandle> IAttrHanflerMap;

    /**
     * 带宽业务模式
     */
    private static String BANDWIDTH_BUSINESS_MODEL = "210004221";
    // 固定
    private static String BANDWIDTH_BUSINESS_MODEL_1 = "110007226";
    // 可变
    private static String BANDWIDTH_BUSINESS_MODEL_2 = "110007227";

    /**
     * 承载电路
     */
    private static String LOAD_CIRCUIT = "210004190";
    // 以太网
    private static String LOAD_CIRCUIT_1 = "110007436";
    // 数字电路
    private static String LOAD_CIRCUIT_2 = "1_0000229";

    /**
     * 电路租用范围
     */
    private static String CIRCUIT_LEASE_RANGE = "2_0000004";
    // 纯本地
    private static String CIRCUIT_LEASE_RANGE_1 = "110007331";

    // 省内长途
    private static String CIRCUIT_LEASE_RANGE_2 = "1_0000114";

    // 省际长途
    private static String CIRCUIT_LEASE_RANGE_3 = "1_0000115";

    /**
     * A端城市
     */
    private static String A_CITY = "2_0000209";

    /**
     * Z端城市
     */
    private static String Z_CITY = "2_0000105";

    /**
     * 固定带宽
     */
    private static String FIXED_BANDWIDTH = "210004224";

    /**
     * 基础宽带
     */
    private static String BASED_BROADBAND = "200001818";

    /**
     * 突发宽带
     */
    private static String SUDDEN_BROADBAND = "210004191";

    /**
     * A端接口类型
     */
    private static String INTERFACE_TYPE_A = "210004203";

    /**
     * Z端接口类型
     */
    private static String INTERFACE_TYPE_Z = "210004212";


    /**
     * 属性值联动处理二次类业务初始化加载，不会执行此方法，
     * 此方法只会在set值触发执行com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler#set
     *
     * @param attr
     * @param inst
     * @param data
     */
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        this.getAttrHander(attr).handler(attr, inst, data);

    }

    public IAttrHandle getAttrHander(KiteAttr attr) {
        if (KiteMapUtils.isEmpty(IAttrHanflerMap)) {
            loadAttrHandleMap();
        }
        IAttrHandle attrHandler = IAttrHanflerMap.get(attr.getFieldName());
        if (attrHandler == null) {
            return new defaultAttr();
        }

        return attrHandler;
    }


    void loadAttrHandleMap() {
        IAttrHanflerMap = new HashMap<String, IAttrHandle>();
        IAttrHanflerMap.put(BANDWIDTH_BUSINESS_MODEL, new BandwidthBusinessModelAttr());
        IAttrHanflerMap.put(LOAD_CIRCUIT, new LoadCircuitAttr());
        IAttrHanflerMap.put(A_CITY, new CityAttr());
        IAttrHanflerMap.put(Z_CITY, new CityAttr());
        IAttrHanflerMap.put(BASED_BROADBAND, new BasedBroadbandAttr());

    }

    /**
     * 属性样式、下拉框联动处理：注意只能处理涉及样式，下拉框联动设值处理，涉及值变更的操作不能在写在此方法里
     * 样式、下拉框处理只要是涉及联动都会触发执行：如二次类业务初始化加载，属性设值（Set方法）
     * com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler#load
     * com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler#set
     *
     * @param attr
     * @param inst
     * @param data
     */
    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    abstract static class IAttrHandle {

        abstract void handler(KiteAttr attr, KiteInst inst, AttrData data);
    }

    /**
     * 处理宽带业务模式联动
     */

    static final class BandwidthBusinessModelAttr extends IAttrHandle {

        @Override
        void handler(KiteAttr attr, KiteInst inst, AttrData data) {
            KiteAttr bandwidthAttr = inst.getAttr(FIXED_BANDWIDTH);
            // 固定宽带时需要处理的联动
            String value = attr.getValue();
            if (KiteStringUtils.isEqual(value, BANDWIDTH_BUSINESS_MODEL_1)) {
                // 固定带宽必填
                bandwidthAttr.setIsNull(IKeyValues.IFTRUE_F);
            }
            // 可变带宽模式时需要处理的联动
            else if (KiteStringUtils.isEqual(value, BANDWIDTH_BUSINESS_MODEL_2)) {
                /**
                 * 突发带宽（仅为突发部分）, 基础带宽,突发结束时间,突发开始时间 需要展示且必填
                 */
                String[] attrFields = {"200001818", "210004306","210004307","210004191", "200001824"};
                for (String attrField : attrFields) {
                    KiteAttr kiteAttr = inst.getAttr(attrField);
                    kiteAttr.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    kiteAttr.setIsNull(IKeyValues.IFTRUE_F);
                }


            }


        }
    }


    /**
     * 处理承载电路类型联动
     */

    static final class LoadCircuitAttr extends IAttrHandle {

        @Override
        void handler(KiteAttr attr, KiteInst inst, AttrData data) {
            KiteAttr bandwidthAttr = inst.getAttr(FIXED_BANDWIDTH);
            KiteAttr aInterfaceType = inst.getAttr(INTERFACE_TYPE_A);
            KiteAttr zInterfaceType = inst.getAttr(INTERFACE_TYPE_Z);
            List<KiteAttrValue> aKiteAttrValues = aInterfaceType.getAttrValues();
            KiteAttr bandwidthBusinessModel = inst.getAttr(BANDWIDTH_BUSINESS_MODEL);
            String bandwidthBusinessModelValue = bandwidthBusinessModel.getValue();
            String value = attr.getValue();
            List<KiteAttrValue> newKiteAttrValues = new ArrayList<KiteAttrValue>();
            // 数字电路时,枚举值仅可选155M/622M/2.5G/10G
            if (KiteStringUtils.isEqual(value, LOAD_CIRCUIT_2)) {
                List<KiteAttrValue> attrValueList = bandwidthAttr.getAttrValues();
                attrValueList.removeIf(attrValue -> !KiteStringUtils.equalsAny(attrValue.getAttrValueName(), "155M", "622M", "2.5G", "10G"));
                // 带宽模式为固定带宽时 承载类型为数字电路接口且带宽模式为固定带宽展示（STM-1电/STM-1光/STM-4光/STM-16光/STM-64光）
                if (KiteStringUtils.equals(bandwidthBusinessModelValue, BANDWIDTH_BUSINESS_MODEL_1)) {

                    newKiteAttrValues = aKiteAttrValues.stream().filter(kiteAttrValue -> KiteStringUtils.equalsAny(kiteAttrValue.getAttrValue(), "110007234", "110007240", "110007235", "110007236", "110007237")).collect(Collectors.toList());
                    aInterfaceType.setAttrValues(newKiteAttrValues);
                    zInterfaceType.setAttrValues(newKiteAttrValues);

                }
            }
            /**
             * 承载电路类型为以太网专线，且带宽模式为固定带宽时，接口类型展示（  FE/GE电口/GE光口/10GE LAN/10GE WAN/100GE）；
             * 承载电路类型为以太网专线，且带宽模式为可调带宽时，接口类型展示（  FE/GE电口/GE光口/10GE LAN/10GE WAN/100GE）；
             */
            else {
                newKiteAttrValues = aKiteAttrValues.stream().filter(kiteAttrValue -> KiteStringUtils.equalsAny(kiteAttrValue.getAttrValue(), "1_0000408", "1_0000409", "110007238", "110007239", "100007382")).collect(Collectors.toList());
                aInterfaceType.setAttrValues(newKiteAttrValues);
                zInterfaceType.setAttrValues(newKiteAttrValues);

            }


        }
    }

    /**
     * 处理承载电路类型联动
     */

    static final class defaultAttr extends IAttrHandle {

        @Override
        void handler(KiteAttr attr, KiteInst inst, AttrData data) {
            // doSomeThing

        }
    }


    /**
     * 处理基础宽带联动
     */

    static final class BasedBroadbandAttr extends IAttrHandle {

        @Override
        void handler(KiteAttr attr, KiteInst inst, AttrData data) {
            // 此联动利旧BasedBandwidthAttrLinkHandler逻辑
        }
    }

    /**
     * AZ端城市校验（电路租用范围选择不同时）
     */

    static final class CityAttr extends IAttrHandle {

        @Override
        void handler(KiteAttr attr, KiteInst inst, AttrData data) {

            /**
             * 校验：选择纯本地时，AZ两端城市为同一个地市；选择省内长途和省际长途时，AZ两端城市为不同城市
             */
            KiteAttr kiteAttr = inst.getAttr(CIRCUIT_LEASE_RANGE);

            KiteAttr aKiteAttr = inst.getAttr(A_CITY);
            KiteAttr zKiteAttr = inst.getAttr(Z_CITY);
            String aCity = aKiteAttr.getValue();
            String zCity = zKiteAttr.getValue();

            String value = kiteAttr.getValue();
            if (KiteStringUtils.isNotEmpty(aCity) && KiteStringUtils.isNotEmpty(zCity)) {
                if (KiteStringUtils.equals(value, CIRCUIT_LEASE_RANGE_1)) {
                    if (!KiteStringUtils.equals(aCity, zCity)) {
                        ErrorConsts.BUSI_CHECK_ERROR.throwOut("电用租用范围为纯本地时， AZ端城市需要一致");
                    }

                } else {
                    if (KiteStringUtils.equals(aCity, zCity)) {
                        ErrorConsts.BUSI_CHECK_ERROR.throwOut("电用租用范围为省内长途或省际时， AZ端城市为不同城市");
                    }
                }
            }
        }
    }


}
